﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Adorners
{
    public class SelectionAdorner : Adorner
    {
        // Be sure to call the base class constructor.
        /// <summary>
        /// 用以表示公式被选取状态的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入公式的“MainBorder”主子元素。</param>
        /// <param name="masterExpression">宿主公式，装饰器就是表示这个公式的被选取状态。</param>
        public SelectionAdorner(UIElement adornedElement, Expressions.Expression masterExpression)
            : base(adornedElement)
        {
            this.masterExpression = masterExpression;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
        }

        private Expressions.Expression masterExpression;

        public Expressions.Expression MasterExpression
        {
            get { return masterExpression; }
        }

        public bool IsSubPanel
        {
            get { return masterExpression == null ? false : (masterExpression is Expressions.SubPanel); }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            // Some arbitrary drawing implements.
            //renderBrush.Opacity = 0.2;

            #region 先画线。
            Pen linePen = null;
            if (!IsSubPanel)
            {
                if (this.masterExpression != null &&
                    this.masterExpression.Editor != null)
                {
                    //linePen = new Pen(Brushes.GreenYellow, 1);
                    linePen = this.masterExpression.Editor.ExSelectionAdornerPen;
                    linePen.DashStyle = DashStyles.Dash;
                }
                else
                {
                    linePen = null;
                }
            }
            else
            {
                if (this.masterExpression != null &&
                    this.masterExpression.Editor != null)
                {
                    //linePen = new Pen(Brushes.HotPink, 1);
                    linePen = this.masterExpression.Editor.SubPanelSelectionAdornerPen;
                    linePen.DashStyle = DashStyles.Dot;
                }
                else
                {
                    linePen = null;
                }
            }

            linePen.LineJoin = PenLineJoin.Round;
            linePen.StartLineCap = PenLineCap.Round;
            linePen.EndLineCap = PenLineCap.Round;

            Point ptTmp = new Point(adornedElementRect.BottomRight.X + 1, adornedElementRect.BottomRight.Y + 1);
            Rect lineRect = new Rect(adornedElementRect.TopLeft, ptTmp);

            //drawingContext.DrawLine(linePen, lineRect.TopLeft, lineRect.TopRight);//顶线
            //drawingContext.DrawLine(linePen, lineRect.TopRight, lineRect.BottomRight);//右线
            //drawingContext.DrawLine(linePen, lineRect.BottomRight, lineRect.BottomLeft);//下线
            //drawingContext.DrawLine(linePen, lineRect.BottomLeft, lineRect.TopLeft);//上线

            //使用这个办法其实比画四条线更好。但要注意，第一个参数必须为：null而不能是：Brshes.Transparent。
            //否则画出来的矩形不是空心的。
            //由于公式需要嵌套，一旦矩形不是空心，则选中外层的公式后，
            //就不能再用鼠标选中内层的公式了。
            drawingContext.DrawRectangle(null, linePen, lineRect);
            #endregion

            #region 再画点。
            //SolidColorBrush renderBrush;
            Brush renderBrush;
            Pen renderPen;
            if (IsSubPanel)
            {
                if (this.masterExpression != null &&
                    this.masterExpression.Editor != null)
                {
                    //renderPen = new Pen(Brushes.HotPink, 1);
                    renderPen = this.masterExpression.Editor.SubPanelSelectionAdornerPen;
                    //renderBrush = new SolidColorBrush(Colors.HotPink);
                    renderBrush = this.masterExpression.Editor.SubPanelSelectionAdornerBrush;
                }
                else
                {
                    renderPen = null;
                    renderBrush = null;
                }
            }
            else
            {
                if (this.masterExpression != null &&
                    this.masterExpression.Editor != null)
                {
                    //renderPen = new Pen(Brushes.GreenYellow, 1);
                    renderPen = this.masterExpression.Editor.ExSelectionAdornerPen;
                    //renderBrush = new SolidColorBrush(Colors.GreenYellow);
                    renderBrush = this.masterExpression.Editor.ExSelectionAdornerBrush;
                }
                else
                {
                    renderPen = null;
                    renderBrush = null;
                }
            }

            drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                new Rect(adornedElementRect.Left - 2, adornedElementRect.Top - 2, 4, 4));//左上
            drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                new Rect(adornedElementRect.Right - 1, adornedElementRect.Top - 2, 4, 4));//右上
            drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                new Rect(adornedElementRect.Left - 2, adornedElementRect.Bottom - 1, 4, 4));//左下
            drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                new Rect(adornedElementRect.Right - 1, adornedElementRect.Bottom - 1, 4, 4));//右下

            #endregion

            #region 最后画备注文本点。

            //文本块会显示备注文本，无必要画点来表示。
            if (masterExpression is Expressions.BaseText == false)
            {
                if (masterExpression.CommentText != null && masterExpression.CommentText.Length > 0)
                {
                    //本来是绘制上下左右中心点，表示备注。但是太复杂，有些令人眼花，所以去掉了三个。
                    //SolidColorBrush commentBrush = new SolidColorBrush(Colors.Yellow);
                    Pen commentPen = new Pen(Brushes.Yellow, 1);

                    drawingContext.DrawRectangle(/*commentBrush*/Brushes.Transparent, commentPen,
                        new Rect(adornedElementRect.Left + 6,
                            adornedElementRect.Top - 2, 4, 4));

                    //drawingContext.DrawRectangle(commentBrush, commentPen,
                    //    new Rect(adornedElementRect.Left + adornedElementRect.Size.Width / 2 - 2,
                    //        adornedElementRect.Top - 2, 4, 4));
                    //drawingContext.DrawRectangle(commentBrush, commentPen,
                    //    new Rect(adornedElementRect.Left + adornedElementRect.Size.Width / 2 - 2,
                    //        adornedElementRect.Bottom - 2, 4, 4));

                    //drawingContext.DrawRectangle(commentBrush, commentPen,
                    //    new Rect(adornedElementRect.Left - 2,
                    //        adornedElementRect.Top + adornedElementRect.Size.Height / 2 - 2, 4, 4));
                    //drawingContext.DrawRectangle(commentBrush, commentPen,
                    //    new Rect(adornedElementRect.Right - 2,
                    //        adornedElementRect.Top + adornedElementRect.Size.Height / 2 - 2, 4, 4));
                }
            }
            #endregion

            #region 如果是文本块，还是根据“ShowBackText”来决定是否绘制一个小三角。
            if (masterExpression is Expressions.BaseText)
            {
                Expressions.BaseText btx = masterExpression as Expressions.BaseText;
                if (btx.ShowBackText)
                {
                    drawingContext.DrawRectangle(/*commentBrush*/Brushes.Transparent, renderPen,
                    new Rect(adornedElementRect.Left - 2,
                        adornedElementRect.Top + 6, 4, 4));
                }
            }
            #endregion
        }
    }
}
